﻿// *************************************************************************************************
// Project: RealSense.Light.Core
// Namespace: RealSense.Light.Core
// File: Session.cs  Author: Peter O'Hanlon
// Copyright © Peter O'Hanlon 2014-2015
// Date: 2015/1/2 13:59
// *************************************************************************************************
using RealSense.Light.Contracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RealSense.Light.Core
{
  public class Session : RealSenseBase, ISession
  {
    private PXCMSession session;
    private ISenseManager senseManager;

    /// <summary>
    /// Create an instance of the Session manager.
    /// </summary>
    public Session()
    {
      // Create the instance of the SDK Context
      session = PXCMSession.CreateInstance();

      // Once we have the session, we can create the PXCMSenseManager instance. This
      // allows us to interact with the various parts of the SDK.
      senseManager = ServiceLocator.Instance.Get<ISenseManager>();
      senseManager.Create(session);

      Version = new Version(session);
    }

    /// <summary>
    /// Gets the SDK version.
    /// </summary>
    public IVersion Version { get; private set; }

    public IEnumerable<string> Devices(DeviceTypes deviceType)
    {
      List<string> devices = new List<string>();

      switch (deviceType)
      {
        case DeviceTypes.SpeechRecognition:
          break;
      }

      return devices;
    }

    /// <summary>
    /// Trigger the RealSense processing.
    /// </summary>
    public void StartProcessing()
    {
      senseManager.StartProcessing();
    }

    private void GetVoiceRecognitionDevices(List<string> devices)
    {
      PXCMAudioSource source = session.CreateAudioSource();
      if (source != null)
      {
        bool success = true;
        source.ScanDevices();
        int index = 0;
        while (success)
        {
          PXCMAudioSource.DeviceInfo deviceInfo = null;
          success = Invoke(() => source.QueryDeviceInfo(index, out deviceInfo));
          if (success && deviceInfo != null)
          {
            devices.Add(deviceInfo.name);
          }
        }
      }
    }

    /// <summary>
    /// Dispose of the session.
    /// </summary>
    protected override void Dispose(bool disposing)
    {
      if (disposing)
      {
        Dispose(senseManager);
        Dispose(session);
      }
    }

    protected void Dispose<T>(T instance) where T : class, IDisposable
    {
      if (instance != null)
      {
        instance.Dispose();
      }
    }
  }
}